﻿using Application.Users.Interfaces;
using Application.Users.Models;
using Application.Users.Specifications;
using MediatR;

namespace Application.Users.Queries.GetUser;


public record GetUserModel
{
    public Guid Id { get; init; }

    public string? UserName { get; init; }

    public string? PhoneNumber { get; init; }

    public string? CollegeName { get; init; }

    public string? MajorName { get; init; }

    public DateTime? CreateTime { get; init; }

    public DateTime? UpdateTime { get; init; }
}

public record GetUserFilter : IRequest<IReadOnlyCollection<GetUserModel>>
{
    public List<Guid>? Ids { get; set; }

    public List<string>? UserNames { get; init; }

    public List<string>? Colleges { get; init; }

    public List<string>? Majors { get; init; }

    public List<string>? PhoneNumbers { get; init; }

    public int PageSize { get; set; }

    public int PageNo { get; set; }
}

/// <summary>
/// 通过id查找用户信息
/// </summary>
public class GetUsersHandler(IUserReadRepository repo) : IRequestHandler<GetUserFilter, IReadOnlyCollection<GetUserModel>>
{
    /// <summary>
    /// 获取用户信息列表
    /// 分页限制：每页最大100条
    /// 过滤筛选限制： 最多允许传入100个筛选条件
    /// </summary>
    /// <param name="request"></param>
    /// <param name="ct"></param>
    /// <returns></returns>
    public async Task<IReadOnlyCollection<GetUserModel>> Handle(GetUserFilter request, CancellationToken ct)
    {
        var user = await repo.GetUsersAsync(new UsersByFilterSpec(request), ct);

        return user;
    }
}